module mod_params
use mod_random

implicit none 


integer :: param_omegaB = 1
integer :: param_omegaNU = 2
integer :: param_w0 = 3
integer :: param_wa = 4
integer :: param_amplitude = 5

type, public :: t_param

   double precision :: val, steplength
   logical :: is_active

end type t_param


type, public :: t_params
      double precision ::  OMEGA,H,OMEGAB, OMEGANU,w0,wa !hentet fra common-blocken i power_nu
      double precision ::  OMEGAL, T_0, N_nu, z, nsval !parametere opprinnelig deklarerte i power_nu
      double precision ::  likelihood
      double precision, dimension(10)  :: params, params_steplength

end type t_params

contains 

subroutine params_setup(params)
  type(t_params), intent(inout) :: params

 ! allocate(params%params(10))
 ! allocate(params%params_steplength(10))

  params%params(1) = params%OMEGAB
  params%params(2) = params%OMEGANU ! 0.01 omega_nu
  params%params(3) = params%w0 ! w0
  params%params(4) = params%wa ! wa
  params%params(5) = 1.0 ! amplitude

!  params%params_steplength(1) = 0.03
!  params%params_steplength(2) = 0.001
!  params%params_steplength(3) = 0.01
!  params%params_steplength(4) = 0.001
!  params%params_steplength(5) = 0

  params%params_steplength(1) = 0.005
  params%params_steplength(2) = 0.003
  params%params_steplength(3) = 0.003
  params%params_steplength(4) = 0.05
  params%params_steplength(5) = 0

end subroutine params_setup


subroutine params_copy(from, to) 
  type(t_params), intent(inout) :: from, to

  to%params(1:5) = from%params(1:5)
  to%params_steplength(1:5) = from%params_steplength(1:5)

end subroutine params_copy

subroutine params_propose(params, params_proposed)
  type(t_params), intent(inout) :: params, params_proposed
  integer :: i
  params_proposed%params_steplength = params%params_steplength
  do i = 1, 5
!     if (i==3) write(*,*) "steplength", params%params_steplength(i)
     params_proposed%params(i) = params%params(i) + &
     & random_normal(0.d0, params%params_steplength(i))
  end do
end subroutine params_propose


subroutine params_print(params) 

  type(t_params), intent(inout) :: params
  write(*,*) params%params(1),params%params(2),params%params(3), params%params(4), params%params(5)   

end subroutine params_print

subroutine params_savetext(params, file, i, filename) 
  type(t_params), intent(inout) :: params
  integer, intent(in) :: file,i
  character(len=127), intent(in) :: filename
  open(unit=file,file=filename,action='write',position='append')
!  write(file,'(I,A,1pE14.7,A,1pE14.7,A,1pE14.7,A,1pE14.7,A,1pE14.7)') &
! &i, ' ',  params%params(1), ' ', params%params(2), ' ', &
!params%params(3), ' ', params%params(4), ' ', params%params(5)   
  write(*,*) i, params%params(1),params%params(2),params%params(3), params%params(4), params%params(5)   
  close(file)

end subroutine params_savetext

end module mod_params
